package com.zkadmin.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.zk.ui.ComponentNotFoundException;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Caption;
import org.zkoss.zul.Center;
import org.zkoss.zul.Include;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.West;
import org.zkoss.zul.Window;
import org.zkoss.zul.impl.LabelImageElement;

public class AdminWebUtils {
	public static void openPage(String zul, String tabName) {
		openPage(zul, tabName, AdminConstants.DEFAULT_ICON, null, false);
	}

	public static void openPage(String zul, String tabName, Map<String, Object> data) {
		openPage(zul, tabName, AdminConstants.DEFAULT_ICON, data, false);
	}

	public static void openPage(String zul, String tabName, String icon) {
		openPage(zul, tabName, icon, null, false);
	}

	public static void openPage(String zul, String tabName, String icon, Map<String, Object> data, boolean reOpen) {
		final Tabs tabs = getTabs();
		Tab openTab = null;
		try {
			openTab = (Tab) tabs.getFellow(createTabId(zul, tabName));
			if (openTab != null) {
				openTab.setSelected(true);
				if (reOpen) {
					openTab.onClose();
					openPage(zul, tabName, icon, data, false);
				}
				return;
			}
		} catch (ComponentNotFoundException ex) {
		}

		int tabNum = tabs.getChildren().size();
		if (tabNum > AdminConstants.MAX_TAB_NUM) {// 默认的开始tab算一个，因此实际数量为此数量加1
			// MsgBox.info("最多只能打开" + (10 + 1) + "个标签页，请关闭不再使用的标签页。");
			return;
		}

		final Tab tab = new Tab();
		tab.setId(createTabId(zul, tabName));
		tab.setLabel(tabName);
		tab.setSelected(true);
		tab.setIconSclass(StringUtils.isEmpty(icon) ? AdminConstants.DEFAULT_ICON : icon);
		tab.setParent(tabs);
		tab.setContext("tabMenuPopup");
		if (!AdminConstants.getHomeLabel().equals(tab.getLabel())) {
			tab.setClosable(true);
			tab.addEventListener(Events.ON_DOUBLE_CLICK, new EventListener<Event>() {
				@Override
				public void onEvent(Event event) throws Exception {
					tab.close();
				}
			});
		}

		Tabpanels tabpanels = getTabpanels();
		Tabpanel tabpanel = new Tabpanel();
		tabpanel.setStyle("overflow:auto;padding:0;");
		tabpanel.setParent(tabpanels);
		if (!zul.startsWith(AdminConstants.PAGE_PATH)) {
			zul = AdminConstants.PAGE_PATH + zul;
		}
		new Include(zul).setParent(tabpanel);
	}

	public static String createTabId(String zulFilePathName, String tabName) {
		return "tab_" + tabName;
	}

	public static Borderlayout getBorderlayout() {
		return ((Borderlayout) Path.getComponent("/mainWindow/mainBorderlayout"));
	}

	public static Tabbox getTabbox() {
		return (Tabbox) getCenter().getFellow("tabbox");
	}

	public static Center getCenter() {
		return getBorderlayout().getCenter();
	}

	public static West getWest() {
		return getBorderlayout().getWest();
	}

	public static Tabs getTabs() {
		return (Tabs) getTabbox().getFellow("tabs");
	}

	public static Tabpanels getTabpanels() {
		return (Tabpanels) getTabbox().getFellow("tabpanels");
	}

	public static void selectedListBox(Listbox listbox, String value) {
		int i = 0;
		for (Listitem lt : (List<Listitem>) listbox.getItems()) {
			if (String.valueOf(value).equals(String.valueOf(lt.getValue()))) {
				listbox.setSelectedIndex(i);
				break;
			}
			i++;
		}
	}

	public static void eventAddCaption(Window window, Event e) {
		if (e.getTarget() instanceof LabelImageElement) {
			LabelImageElement et = (LabelImageElement) e.getTarget();
			AdminWebUtils.addCaption(window, et.getIconSclass());
		}
	}

	public static void addCaption(Window win, String icon) {
		addCaption(win, null, icon);
	}

	public static void addCaption(Window win, String subTitle, String icon) {
		Caption caption = new Caption();
		caption.setIconSclass(icon);
		if (subTitle != null) {
			caption.setLabel(subTitle);
		}
		win.appendChild(caption);
	}

	public static String getZulPagePath(String zul) {
		return AdminConstants.PAGE_PATH + zul;
	}
}
